package com.visionblue.business.ship.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author: xlh
 * @date: 2020-04-14 1:23 PM
 * @desc:
 */
public class FormatUtils {

    private static final SimpleDateFormat DATE_FORMAT2 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private static final SimpleDateFormat DATE_FORMAT3 = new SimpleDateFormat("MM-dd", Locale.getDefault());
    private static final SimpleDateFormat DATE_FORMAT4 = new SimpleDateFormat("MM月dd日", Locale.getDefault());
    private static final SimpleDateFormat DATE_FORMAT5 = new SimpleDateFormat("HH:mm", Locale.getDefault());
    private static final SimpleDateFormat DATE_FORMAT6 = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
    private static final SimpleDateFormat DATE_FORMAT7 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.000");

    public static String formatTimeMMDD(String timeStamp) {

        try {
            long value = Long.parseLong(timeStamp);
            return DATE_FORMAT3.format(new Date(value));
        } catch (NumberFormatException e) {
            return timeStamp;
        }
    }

    public static String formatTimeMMDD2(String timeStamp) {

        try {
            long value = Long.parseLong(timeStamp);
            return DATE_FORMAT4.format(new Date(value));
        } catch (NumberFormatException e) {
            return timeStamp;
        }
    }

    public static String formatTimeYMDHHmm(Long value) {
        try {
            return DATE_FORMAT2.format(new Date(value));
        } catch (NumberFormatException e) {
            return String.valueOf(value);
        }
    }

    public static String formatTimeHHmmss(Long value) {
        try {
            return DATE_FORMAT6.format(new Date(value));
        } catch (NumberFormatException e) {
            return String.valueOf(value);
        }
    }

    public static String formatTimeHHmm(Long value) {
        try {
            return DATE_FORMAT5.format(new Date(value));
        } catch (NumberFormatException e) {
            return String.valueOf(value);
        }
    }



    public static String formatTimeYMD(Long timestamp) {
        try {
            return DATE_FORMAT7.format(new Date(timestamp));
        } catch (NumberFormatException e) {
            return String.valueOf(timestamp);
        }
    }

    public static String formatGps(String gps) {
        try {
            String[] strs = gps.split("\\.");
            BigDecimal intPart = new BigDecimal(strs[0]);
            BigDecimal wholeVal = new BigDecimal(gps);
            BigDecimal minutes = wholeVal.subtract(intPart).multiply(new BigDecimal(60)).setScale(3, RoundingMode.HALF_DOWN);
            return intPart.toPlainString() + "°" + minutes.toPlainString() + "\'";
        } catch (Exception e) {
            return gps;
        }
    }

    public static String format2Decimal(Double value) {
        return new BigDecimal(value).setScale(2, RoundingMode.HALF_DOWN).toPlainString();
    }

    public static String format3Decimal(Double value) {
        return new BigDecimal(value).setScale(3, RoundingMode.HALF_DOWN).toPlainString();
        //return new BigDecimal(value*Math.pow(10,39)).setScale(3, RoundingMode.HALF_DOWN).toPlainString();
    }

    public static String formatDecimal3Text(Double value) {
        try {
            DECIMAL_FORMAT.setRoundingMode(RoundingMode.HALF_DOWN);
            return DECIMAL_FORMAT.format(value);
        } catch (Exception e) {
            return String.valueOf(value);
        }

    }

    public static String encrypt(String text, int len) {
        StringBuilder sb = new StringBuilder();
        if (text.length() > len) {
            return sb.append(text.substring(0, len)).append("...").toString();
        } else {
            return text;
        }
    }
}
